﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Common;
using AspNetCs.Common;
using AspNetCs.Bll;
using AspNetCs.Model;
using System.Data.SqlClient;
using AspNetCs.Config;


public partial class _Default :BasePage
{


    private int pageSize = 40;

    private LoveWallBLL loveWallBLL = new LoveWallBLL();
    //RequestHelper req = new RequestHelper();
    //Utils utils = new Utils();
    public string getCount;
    public string str = "";//用于存放最新的纸条信息
    public string initNO = "0";

    public int pageIndex = 1;
    public string keyWord = "";

    //查询记录的条数
    public int totalCount = 0;


    protected void Page_Load(object sender, EventArgs e)
    {
        
        if(Request["keyWord"]!=null)
        {
            keyWord = Request["keyWord"].ToString();
        }
        if (Request["pageIndex"] != null)
        {
            try
            {
                pageIndex = Convert.ToInt32(Request["pageIndex"]);
            }
            catch(Exception ex)
            {
                 new CustomException(ex);
            }
        }
        //string no = Common.RequestHelper.GetString("no");
        ////Response.Write(no);
        //string sqlStr = "";
        //if (no.Length>0)
        //{
        //    if (no.Length >=6)
        //    {
        //        sqlStr = "select top 1 * from love where send like '%" + no + "%'  or pick like '%" + no + "%' order by id desc";
        //    }
        //    else
        //    {
        //        sqlStr = "select top 1 * from love where id=" + Common.RequestHelper.GetInt("no", 0) + " order by id desc";
        //    }
        //    Socut.Reader sr = new Socut.Reader(sqlStr);
        //    if (sr.Read())
        //    {
        //        initNO=sr["id"].ToString ();
        //    }
        //    else
        //    {
        //        initNO = Common.RequestHelper.GetInt("no", 1).ToString();
        //    }
        //    sr.Close();
        //}

        //统计贴纸数
        //getCount = Socut.Data.ExecuteScalar("select count(id) from love").ToString ();
        getCount = loveWallBLL.GetLoveWallMsgNum().ToString();
        Random r = new Random();

        SqlDataReader reader = loveWallBLL.getGetLoveWallMsg(keyWord, pageIndex, pageSize, 1);
        if(reader.Read())
        {
            totalCount = reader.GetInt32(0);
        }
        reader.Close();
        System.Data.SqlClient.SqlDataReader sr1 = loveWallBLL.getGetLoveWallMsg(keyWord, pageIndex, pageSize, 0);
        int i=0;
        while (sr1.Read())
        {
            //设置贴纸的位置
            int T = int.Parse((38 * r.Next(8) + 80).ToString());
            int L = int.Parse((74 * r.Next(8) + 15).ToString ());
            string mobile = "";
            //if (sr1["ismobile"]==true )//是否是小灵通发送的
            //{
            //    mobile="<a href='#' onmousemove=QQLoveScripShow('Tips',event) onmouseout=QQLoveScripHide('Tips')>用小灵通发表字条</a>";
            //}
            //str += "sArray[" + i + "]=new Array('" + sr1["id"] + "','" + sr1["Face"] + "','" + L + "','" + T + "','images/icon" + sr1["Icon"] + ".gif','" + getpublic(sr1["ismobile"], sr1["Pick"]) + "','" + nenicon(sr1["Info"].ToString ()) + "','" + getpublic(sr1["ismobile"], sr1["Send"]) + "','" + sr1["PostDate"] + "','" + 30 - i + "','','" + mobile + "');" + vbCrLf & vbCrLf;
            string id = sr1["Id"].ToString();
            string pDate = sr1["PDate"].ToString();
            string content = sr1["Content"].ToString();
            string author = sr1["Author"].ToString();
            string bgColor = sr1["BgColor"].ToString();
            string bgImg = sr1["BgImg"].ToString();
            str += "sArray[" + i + "]=new Array('" +id+ "','" + bgColor + "','" + L + "','" + T + "','images/icon" + bgImg+ ".gif','接收者','" +showIcon ( content) + "','" + author+ "','" + pDate+ "','" + (30 - i) + "','','" + mobile + "');" + "\r\n";
            
            i += 1;

        }
        sr1.Close();
    }

    private string nenicon(object p)
    {
        throw new NotImplementedException();
    }

    private string getpublic(object p, object p_2)
    {
        throw new NotImplementedException();
    }

    public string getpublic(bool ismobile,string username) {
        if (ismobile==true && username.Trim().Length >=11)
        {
            return username.Substring(0, 4) + "***" + username.Substring(8,4);
        }
        else
        {
            return username;
        }
    }

    public string showIcon(string str)
    {
        string str1 = str;
        str1 = str1.Replace("[em]", "<img src=msg/").Replace("[/em]", ".gif />");
        return str1;
    }
}
